package com.game_info.controller;

import com.game_info.service.GamesMsgService;
import com.game_info.service.UsersService;
import com.game_info.utils.AjaxResult;
import com.game_info.vo.GamesMsg;
import com.game_info.vo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

/**
 * 普通用户功能控制器
 *
 * @author ttj
 * @Date 2020-4-15
 */
@Controller
@RequestMapping("/publicUser")
public class PublicUserController {

    @Autowired
    private GamesMsgService gamesMsgService;
    @Autowired
    private UsersService usersService;

    /**
     * 用户登陆
     * resCode说明:
     * 1:登陆成功
     * 2:用户名密码不能为空
     * 0或null:用户名密码输入错误
     *
     * 返回data:base64加密用户id用作已登陆标识
     *
     * @param uName
     * @param uPwd
     * @return
     */
    @PostMapping("/userLogin.do")
    @ResponseBody
    public AjaxResult userLogin(String uName, String uPwd) throws UnsupportedEncodingException {
        String msg = null;
        Integer resCode = null;
        String loginId=null;
        if (uName != null && uName != "" && uPwd != null && uPwd != "") {
            resCode = usersService.selectUserByIdOrName(uName, uPwd);
            if (resCode == 0 || resCode == null) {
                msg = "登陆失败,用户名或密码输入错误";
            } else if (resCode == 1) {
                msg = "登陆成功";
                // BASE64加密
                Base64.Encoder encoder = Base64.getEncoder();
                byte[] data = encoder.encode(("Logined:"+uName).getBytes("UTF-8"));
                loginId= new String(data);

            }
        } else {
            msg = "用户名密码不能为空";
        }
        System.out.println("BASE64加密用户名：" + loginId);
        return AjaxResult.put(loginId, msg);
    }

    /**
     * 用户注册
     * 性别默认未知
     * 喜爱的游戏默认空
     *
     * @param uName
     * @param uPwd
     * @return
     */
    @PostMapping("/userReg.do")
    @ResponseBody
    public AjaxResult userReg(String uName, String uPwd) {
        String msg = null;
        Integer resCode=0;
        if (uName != null && uName != "" && uPwd != null && uPwd != "") {
            Integer resCodeNname = usersService.selectUserByIdOrName(uName, null);
            if (resCodeNname!=1){
                Users users = new Users();
                users.setuName(uName);
                users.setuPwd(uPwd);
                users.setuSex("未知");
                users.setuGames(" ^ ^ ");
                resCode=usersService.insertUsers(users);
            if (resCode==1){
                msg="注册成功";
            }else {
                msg="注册失败";
            }
            }else {
                msg="当前用户名已被注册";
            }
        }else{
            msg="注册用户名或密码不能为空";
        }
        return AjaxResult.put(resCode,msg);
    }

    /**
     * 根据用户名查询用户信息(不包含密码)
     * @param uName
     * @return
     */
    @PostMapping("/selectUserInfoByName.do")
    @ResponseBody
    public AjaxResult selectUserInfoByName(String uName){
        Users user=usersService.selectUserByName(uName);
        return AjaxResult.put(user,"根据用户名查询用户信息执行成功");
    }

    /**
     * 修改用户信息(不包含密码)
     * @param users
     * @return
     */
    @PostMapping("/updateUserInfo.do")
    @ResponseBody
    public AjaxResult updateUserInfo(Users users){
        Integer resCode=usersService.updateUsers(users);
        String msg=null;
        if (resCode==1){
            msg="修改成功";
        }else{
            msg="修改失败";
        }
        return AjaxResult.put(resCode,msg);
    }

    /**
     * 修改密码
     * @param uName
     * @param uPwd
     * @param newUPwd
     * @return
     */
    @PostMapping("/updateUserPwd.do")
    @ResponseBody
    public AjaxResult updateUserPwd(String uName,String uPwd,String newUPwd){
        String msg = null;
        Integer resCode = null;
        Integer resCode_upd = null;
        String loginId=null;
        if (uName != null && uName != "" && uPwd != null && uPwd != "") {
            resCode = usersService.selectUserByIdOrName(uName, uPwd);
            if (resCode == 0 || resCode == null) {
                msg = "修改失败,原用户名或密码输入错误";
            } else if (resCode == 1) {
                Users user=usersService.selectUserByName(uName);
                user.setuPwd(newUPwd);
                resCode_upd=usersService.updateUsers(user);
                if (resCode_upd==0){
                    msg="修改失败";
                }else if(resCode_upd==1){
                    msg="修改成功";
                }
            }
        } else {
            msg = "用户名密码不能为空";
        }
        return AjaxResult.put(resCode_upd,msg);
    }

    /**
     * 用户新增评论
     *
     * @param gamesMsg
     * @return
     */
    @PostMapping("/insertMsg.do")
    @ResponseBody
    public AjaxResult insertMsg(@RequestBody GamesMsg gamesMsg) {
        int result = 0;//新增执行结果
        String msg = null;//信息提示
        int count = 0;//当前游戏留言总数
        if (gamesMsg != null && gamesMsg.getMsgContent() != null && gamesMsg.getMsgContent() != "") {//内容不为空
            gamesMsgService.insertGamesMsg(gamesMsg);
            count = gamesMsgService.selectCountByMsgBelong(gamesMsg.getMsgBelong());
            msg = "评论成功";
        } else {
            count = gamesMsgService.selectCountByMsgBelong(gamesMsg.getMsgBelong());
            msg = "评论失败";
        }
        return AjaxResult.put(result, msg, count);
    }
}
